package com.lplb.common.core.result;


import lombok.Data;

/**
 * HTTP 请求返回类 (Restful-API 接口统一参数返回)
 *
 * @author 王松
 * @mail 1720696548@qq.com
 * @date 2020/7/13 0013 10:39
 */
@Data
public class R<T> {


    private static final long serialVersionUID = -4823388468910670152L;
    /**
     * 响应结果码
     */
    private Integer code;
    /**
     * 响应结果数据
     */
    private String msg;
    /**
     * 响应内容
     */
    private T data;
    /**
     * 响应错误信息（内存栈）
     */
    private String errorMsg;


    /**
     * 信息返回
     */
    public R(Integer code, String msg, T data, String errorMsg) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.errorMsg = errorMsg;
    }


    // 返回成功
    public static R<Void> success() {
        return new R(RType.SYS_SUCCESS.getCode(), RType.SYS_SUCCESS.getMsg(), null, null);
    }

    public static <T> R<T> success(T data) {
        return new R(RType.SYS_SUCCESS.getCode(), RType.SYS_SUCCESS.getMsg(), data, null);
    }


    //  通用返回失败
    public static <T> R<T> failure(RType rType) {
        return new R(rType.getCode(), rType.getMsg(), null, null);
    }

    public static <T> R<T> failure(RType rType, String errorMsg) {
        return new R(rType.getCode(), rType.getMsg(), null, errorMsg);
    }

    public static <T> R<T> failure(Integer code, String msg, String errorMsg) {
        return new R(code, msg, null, errorMsg);
    }
}

